package org.me.marble;

import java.nio.*;

import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;

/**
*
* @author tungi
*/

public class Path extends GameObject{
    public ArrayList<Triangle> triangles;
    
    private FloatBuffer _vertexBuffer;//coordinates of triangle
    private FloatBuffer _textureBuffer;
    private FloatBuffer _normalBuffer;

    private int points=0;

    public Path(int id){
        super(id);
        triangles=new ArrayList<Triangle>();
    }

    @Override
    public void init(){
        points=triangles.size()*3;
        
        float[] coords=new float[points*3];
        int i=0;
        for(i=0;i<triangles.size();++i){
            coords[i*9]=triangles.get(i).getPoint(0).getX();
            coords[i*9+1]=triangles.get(i).getPoint(0).getY();
            coords[i*9+2]=triangles.get(i).getPoint(0).getZ();
            coords[i*9+3]=triangles.get(i).getPoint(1).getX();
            coords[i*9+4]=triangles.get(i).getPoint(1).getY();
            coords[i*9+5]=triangles.get(i).getPoint(1).getZ();
            coords[i*9+6]=triangles.get(i).getPoint(2).getX();
            coords[i*9+7]=triangles.get(i).getPoint(2).getY();
            coords[i*9+8]=triangles.get(i).getPoint(2).getZ();
        }
        
        float[] normals=new float[points*3];
        for(i=0;i<triangles.size();++i){
            normals[i*9]=normals[i*9+3]=normals[i*9+6]=
                    triangles.get(i).getNormal().getX();
            normals[i*9+1]=normals[i*9+4]=normals[i*9+7]=
                    triangles.get(i).getNormal().getY();
            normals[i*9+2]=normals[i*9+5]=normals[i*9+8]=
                    triangles.get(i).getNormal().getZ();
        }

        float right=0.0f,left=0.0f,d_left=1.0f,d_right=1.0f;
        float[] textures=new float[points*2];
        for(i=0;i<triangles.size()/2;++i){
            d_right=Vector.Minus(triangles.get(i*2+1).getPoint(0),
                    triangles.get(i*2+1).getPoint(1)).Length()/4;
            d_left=Vector.Minus(triangles.get(i*2).getPoint(0),
                    triangles.get(i*2).getPoint(1)).Length()/4;
            d_left=d_right=Math.max(d_left,d_right);

            textures[i*12]=0.0f;
            textures[i*12+1]=left;
            textures[i*12+2]=0.0f;
            textures[i*12+3]=left+d_left;
            textures[i*12+4]=1.0f;
            textures[i*12+5]=right;

            textures[i*12+6]=1.0f;
            textures[i*12+7]=right+d_right;
            textures[i*12+8]=1.0f;
            textures[i*12+9]=right;
            textures[i*12+10]=0.0f;
            textures[i*12+11]=left+d_left;

            left+=d_left;
            right+=d_right;
        }

        _vertexBuffer=BufferFactory.MakeFloatBuffer(coords);
        _normalBuffer=BufferFactory.MakeFloatBuffer(normals);
        _textureBuffer=BufferFactory.MakeFloatBuffer(textures);

        color=new MyColor();
        color.setAmbient(new float[]{(float)226/255,(float)176/255,(float)112/255,1.0f});
        color.setDiffuse(new float[]{(float)226/255,(float)176/255,(float)112/255,1.0f});
        color.setSpecular(new float[]{0.6f,0.6f,0.6f,1.0f});
        color.setEmission(new float[]{0.1f,0.1f,0.1f,1.0f});
        color.setShininess(1.0f);
    }

    @Override
    public void draw(GL10 gl){
        OpenGLSystem.enable(gl,OpenGLSystem.TEXTURE);//CULL_FACE
        OpenGLSystem.enableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | 
                OpenGLSystem.TEXTURE_ARRAY | OpenGLSystem.VERTEX_ARRAY);

        OpenGLSystem.setMaterial(gl,color);
        OpenGLSystem.drawTriangles(gl,texture,_vertexBuffer,_normalBuffer,
                _textureBuffer,points,GL10.GL_TRIANGLES);

        OpenGLSystem.disableClientStates(gl,OpenGLSystem.NORMAL_ARRAY |
                OpenGLSystem.TEXTURE_ARRAY | OpenGLSystem.VERTEX_ARRAY);
        OpenGLSystem.disable(gl,OpenGLSystem.TEXTURE);
    }
}
